package com.oceansoft.core.message;

import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class EncryptionUtil {

	// 约定默认KEY
	private static String KEY = "Oceansoft";

	// 根据KEY获取8位的byte[]
	private static byte[] getKey(String key) {
		byte[] result = new byte[8];
		StringBuilder sb = new StringBuilder();
		sb.append(key).append("12345678");

		try {
			result = sb.substring(0, 8).toString().getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 加密
	 * 
	 * @param originalStr
	 * @return
	 */
	public static String desEncrypt(String originalStr, String iv) {
		String result = null;
		byte[] tmpOriginalStr = null;
		try {
			tmpOriginalStr = originalStr.getBytes("UTF-8");
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			DESKeySpec dks = new DESKeySpec(getKey(KEY));
			SecretKey secretKey = keyFactory.generateSecret(dks);
			IvParameterSpec param = new IvParameterSpec(getKey(iv));
			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, param);
			byte[] tmpEncypt = cipher.doFinal(tmpOriginalStr);
			if (tmpEncypt != null) {
				result = Base64.encodeBytes(tmpEncypt);
			}

		} catch (Exception e) {
		}
		return result;
	}

	/**
	 * 加密
	 * 
	 * @param originalStr
	 * @return
	 */
	public static String encrypt(String originalStr) {
		String result = null;
		byte[] tmpOriginalStr = null;
		try {
			tmpOriginalStr = originalStr.getBytes("UTF-8");
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			DESKeySpec dks = new DESKeySpec(getKey(KEY));
			SecretKey secretKey = keyFactory.generateSecret(dks);
			IvParameterSpec param = new IvParameterSpec(getKey(KEY));
			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, param);
			byte[] tmpEncypt = cipher.doFinal(tmpOriginalStr);
			if (tmpEncypt != null) {
				result = Base64.encodeBytes(tmpEncypt);
			}

		} catch (Exception e) {
		}
		return result;
	}

	/**
	 * 解密
	 * 
	 * @param encrypStr
	 * @return
	 */
	public static String desDecrypt(String encrypStr, String iv) {
		String result = null;
		byte[] tmpEncrypStr = null;
		try {
			tmpEncrypStr = Base64.decode(encrypStr);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			DESKeySpec dks = new DESKeySpec(getKey(KEY));
			SecretKey secretKey = keyFactory.generateSecret(dks);
			IvParameterSpec param = new IvParameterSpec(getKey(iv));
			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, secretKey, param);
			byte[] tmpDecrypt = cipher.doFinal(tmpEncrypStr);
			if (tmpDecrypt != null) {
				result = new String(tmpDecrypt, "UTF-8");
			}

		} catch (Exception e) {
		}
		return result;
	}

	/**
	 * 解密
	 * 
	 * @param encrypStr
	 * @return
	 */
	public static String decrypt(String encrypStr) {
		String result = null;
		byte[] tmpEncrypStr = null;
		try {
			tmpEncrypStr = Base64.decode(encrypStr);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			DESKeySpec dks = new DESKeySpec(getKey(KEY));
			SecretKey secretKey = keyFactory.generateSecret(dks);
			IvParameterSpec param = new IvParameterSpec(getKey(KEY));
			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, secretKey, param);
			byte[] tmpDecrypt = cipher.doFinal(tmpEncrypStr);
			if (tmpDecrypt != null) {
				result = new String(tmpDecrypt, "UTF-8");
			}

		} catch (Exception e) {
		}
		return result;
	}

	// 测试方法
	public static void main(String[] args) {
		// String sig = EncryptionUtil.encrypt("wangzhi", "123456789");
		//
		// // 加密
		// System.out.println(sig);
		// try {
		// System.out.println(URLEncoder.encode(sig, "UTF-8"));
		// } catch (UnsupportedEncodingException e) {
		// e.printStackTrace();
		// }

		// // 解密
		// System.out.println(EncryptionUtil.decrypt("P95tyY/RLks=",
		// "aaaaaaaa"));

	}
}
